package com.store.app.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.OrderBean;

import org.json.JSONObject;

import java.util.List;

public class OrderAdapter extends BaseAdapter {
    private List<OrderBean> goodsList;
    private Context context;
    private OnClickOrder onClickOrder;

    public interface OnClickOrder {
        void cancleClick(OrderBean bean);//取消

        void confirmClick(OrderBean bean);//确认

        void detailsClick(OrderBean bean);//查看详情
    }

    public OrderAdapter(List<OrderBean> goodsList, Context context, OnClickOrder onClickOrder) {
        this.goodsList = goodsList;
        this.context = context;
        this.onClickOrder = onClickOrder;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final OrderBean orderBean = goodsList.get(position);


        convertView = LayoutInflater.from(context).inflate(R.layout.item_hpt_order, null);
        TextView order_title = (TextView) convertView.findViewById(R.id.order_title);
        TextView order_id = (TextView) convertView.findViewById(R.id.order_id);
        TextView order_pay_money = (TextView) convertView.findViewById(R.id.order_pay_money);
        TextView order_count = (TextView) convertView.findViewById(R.id.order_count);
        TextView order_date = (TextView) convertView.findViewById(R.id.order_date);
        TextView order_create_date = (TextView) convertView.findViewById(R.id.order_create_date);
        TextView tv_status = (TextView) convertView.findViewById(R.id.tv_status);
        TextView order_detail = (TextView) convertView.findViewById(R.id.order_detail);
        TextView order_btn_cancle = (TextView) convertView.findViewById(R.id.order_btn_cancle);
        TextView order_btn_ok = (TextView) convertView.findViewById(R.id.order_btn_ok);
//            TextView order_name = (TextView) convertView.findViewById(R.id.order_name);
        TextView order_pifa_price = (TextView) convertView.findViewById(R.id.order_pifa_price);
        TextView order_logistics = (TextView) convertView.findViewById(R.id.order_logistics);//物流
        TextView tv_loaded_code = (TextView) convertView.findViewById(R.id.tv_loaded_code);
        LinearLayout ll_phone = (LinearLayout) convertView.findViewById(R.id.ll_phone);
        ImageView order_img = (ImageView) convertView.findViewById(R.id.order_img);

        if (!TextUtils.isEmpty(orderBean.getItem_contact_person()) && !TextUtils.isEmpty(orderBean.getItem_contact_tel())) {
            ll_phone.setVisibility(View.VISIBLE);
        } else {
            ll_phone.setVisibility(View.GONE);
        }
        ll_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = orderBean.getItem_contact_person();
                if (TextUtils.isEmpty(name)) {
                    name = "";
                }
                String tel = orderBean.getItem_contact_tel();
                if (TextUtils.isEmpty(tel)) {
                    tel = "";
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.
                        setMessage("是否拨打 " + tel)
                        .setNegativeButton("取消", null);

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //用intent启动拨打电话
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + orderBean.getItem_contact_tel()));
                        context.startActivity(intent);
                    }
                });
                builder.show();
            }
        });
        order_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.detailsClick(orderBean);
            }
        });
        order_btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.cancleClick(orderBean);
            }
        });
        order_btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.confirmClick(orderBean);
            }
        });

        order_title.setText(orderBean.getItem_goods_title());
        order_id.setText("订单编号: " + orderBean.getOrder_no());
        order_pay_money.setText("团购价格: " + orderBean.getRetail_price());
        order_count.setText("成团数量: " + orderBean.getQty_limit());
        order_date.setText("截止时间: " + orderBean.getClosing_time());
        order_create_date.setText("创建时间: " + orderBean.getOrder_date());
        order_pifa_price.setText("批发价格: " + orderBean.getItem_discount_fee());
//            order_name.setText(orderBean.getItem_contact_person());
        ImageLoader.getInstance().displayImage(orderBean.getGoods_pic_info_url(), order_img, options);
        String status = orderBean.getStatus();
        if(TextUtils.isEmpty(orderBean.getLoaded_code())){
            tv_loaded_code.setVisibility(View.GONE);
        }else{
          tv_loaded_code.setVisibility(View.VISIBLE);
            tv_loaded_code.setText("提货码 : "+orderBean.getLoaded_code());
        }

        //物流信息
        try {
            String s_logistics = orderBean.getLogistics();
            JSONObject log_obj = new JSONObject(s_logistics);
            String s_company = log_obj.getString("company");
            String s_number = log_obj.getString("number");
            if (!TextUtils.isEmpty(s_company) && !TextUtils.isEmpty(s_number)) {
                order_logistics.setVisibility(View.VISIBLE);
                order_logistics.setText("物流：" + s_company + "  " + s_number);
            } else {
                order_logistics.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            order_logistics.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(status)) {
            if (status.equals("activing")) {
                tv_status.setText("预售中");
                order_detail.setVisibility(View.VISIBLE);
                order_btn_cancle.setVisibility(View.VISIBLE);
                order_btn_ok.setVisibility(View.GONE);
            } else if (status.equals("confirmed")) {
//                    tvcheck.setText("订单确定");
//                    tvbtn_check.setVisibility(View.VISIBLE);
//                    tvbtn_check.setText("取消订单");
            } else if (status.equals("delivered")) {
                tv_status.setText("已发货");
                order_detail.setVisibility(View.VISIBLE);
                order_btn_ok.setVisibility(View.VISIBLE);
                order_btn_cancle.setVisibility(View.GONE);
            } else if (status.equals("payed")) {
                tv_status.setText("已支付");
                order_detail.setVisibility(View.VISIBLE);
                order_btn_ok.setVisibility(View.GONE);
                order_btn_cancle.setVisibility(View.GONE);
            } else if (status.equals("received")) {
                tv_status.setText("已收货");
                order_detail.setVisibility(View.VISIBLE);
                order_btn_ok.setVisibility(View.GONE);
                order_btn_cancle.setVisibility(View.GONE);
            } else if (status.equals("cancelled")) {
                tv_status.setText("已取消");
                order_detail.setVisibility(View.VISIBLE);
                order_btn_ok.setVisibility(View.GONE);
                order_btn_cancle.setVisibility(View.GONE);
            } else {
                tv_status.setText("");
                order_detail.setVisibility(View.GONE);
                order_btn_ok.setVisibility(View.GONE);
                order_btn_cancle.setVisibility(View.GONE);
            }
        }
        ll_phone.setVisibility(View.GONE);


        return convertView;
    }


    class ViewHolder {
        private ImageView img;
        private TextView title;
        private TextView numqry;
        private TextView name;
        private TextView date;
        private TextView num;
        private TextView price;
        private TextView check;
        private TextView id;
        private TextView btn_check;
        private TextView take_phone;
        private TextView logistics;
        private ImageView title_coin;
        private ImageView price_coin;
        private TextView tvPayWay;
        private TextView tvOrderMessage;//订单留言
        private TextView tv_loaded_code;//提货码
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
